/*
 * Brian M. Luger
 * South Welcome Panel
 * Date: 12/3/2011
 */
package gui.welcome;

import gui.CAFrame;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import caobject.Conference;
import caobject.EnumRole;
import caobject.User;

/**
 * South Welcome Panel
 * 
 * @author Brian M. Luger
 * @version 12/3/2011
 */
@SuppressWarnings("serial")
public class WelcomeSouthPanel extends JPanel {
	/**
	 * Parent Frame
	 */
	private final CAFrame my_parent;
	
	/**
	 * Parent Panel
	 */
	private final JPanel my_parentPanel;
	
	/**
	 * The Conference List
	 */
	private final ArrayList<Conference> my_confData;
	
	/**
	 * Chosen Conference
	 */
	private Conference my_conference;
	
	/**
	 * Chosen User
	 */
	private User my_user;
	
	/**
	 * Chosen Role
	 */
	private EnumRole my_role;
	
	/**
	 * Conference ComboBox
	 */ 
	private final JComboBox my_confComboBox;
	
	/**
	 * User ComboBox
	 */
	private final JComboBox my_userComboBox;
	
	/**
	 * Role ComboBox
	 */
	private final JComboBox my_roleComboBox;
	
	/**
	 * Go Button
	 */
	private final JButton my_goButton;

	/**
	 * WelcomeSouthPanel Constructor
	 * 
	 * @param parentFrame Parent Frame
	 */
	public WelcomeSouthPanel(final CAFrame parentFrame,
			final JPanel parentPanel,
			final ArrayList<Conference> the_confData) {
		my_parent = parentFrame;
		my_parentPanel = parentPanel;
		my_confData = the_confData;
		
		my_confComboBox = new JComboBox();
		my_userComboBox = new JComboBox();
		my_roleComboBox = new JComboBox();
		
		my_goButton = new JButton();
		
		setLayout(new GridLayout(2,2));
		
		init();
	}
	
	/**
	 * Panel Initialization
	 */
	private void init() {
		setBackground(new Color(255,255,255,0));
		confCBInit();
		userCBInit();
		roleCBInit();
		goButtonInit();
		
		// Conference ComboBox ----------------------------
		final JPanel gridOnePanel = new JPanel();
		gridOnePanel.setBackground(new Color(255,255,255,0));
		gridOnePanel.setLayout(
				new FlowLayout(FlowLayout.CENTER));
		final JLabel confLabel = new JLabel("Conference: ");
		confLabel.setFont(new Font("Times New Roman", Font.BOLD, 20));
		gridOnePanel.add(confLabel);
		my_confComboBox.setEditable(false);
		gridOnePanel.add(my_confComboBox);
		add(gridOnePanel);
		// ------------------------------------------------
		
		// User ComboBox ----------------------------------
		final JPanel gridTwoPanel = new JPanel();
		gridTwoPanel.setBackground(new Color(255,255,255,0));
		gridTwoPanel.setLayout(
				new FlowLayout(FlowLayout.CENTER));
		final JLabel userLabel = new JLabel("User: ");
		userLabel.setFont(new Font("Times New Roman", Font.BOLD, 20));
		gridTwoPanel.add(userLabel);
		my_userComboBox.setEditable(false);
		gridTwoPanel.add(my_userComboBox);
		add(gridTwoPanel);
		// ------------------------------------------------
		
		// Role ComboBox ----------------------------------
		final JPanel gridThreePanel = new JPanel();
		gridThreePanel.setBackground(new Color(255,255,255,0));
		gridThreePanel.setLayout(
				new FlowLayout(FlowLayout.CENTER));
		final JLabel roleLabel = new JLabel("Role: ");
		roleLabel.setFont(new Font("Times New Roman", Font.BOLD, 20));
		gridThreePanel.add(roleLabel);
		my_roleComboBox.setEditable(false);
		gridThreePanel.add(my_roleComboBox);
		add(gridThreePanel);
		// ------------------------------------------------
		
		// Go! Button -------------------------------------
		final JPanel gridFourPanel = new JPanel();
		gridFourPanel.setBackground(
				new Color(255,255,255,0));
		gridFourPanel.setLayout(
				new FlowLayout(FlowLayout.CENTER));
		gridFourPanel.add(my_goButton);
		add(gridFourPanel);
		// ------------------------------------------------
	}
	
	/**
	 * Conference ComboBox Initialization
	 */
	private void confCBInit() {
		my_confComboBox.addItem("Select a Conference...");
		for (Conference conf : my_confData) {
			my_confComboBox.addItem(conf.getTitle());
		}
		
		my_confComboBox.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				final int selection = 
					my_confComboBox.getSelectedIndex();
				
				if (selection > 0) {
					my_conference = my_confData.get(selection - 1);
					my_userComboBox.setEnabled(true);
					userCBInit();
				} else {
					my_userComboBox.setEnabled(false);
					my_roleComboBox.setEnabled(false);
					my_goButton.setEnabled(false);
				}
			}
		});
	}
	
	/**
	 * User ComboBox Initialization
	 */
	private void userCBInit() {
		my_userComboBox.removeAllItems();
		my_userComboBox.addItem("Select a User...");
		if (my_conference == null) {
			my_userComboBox.setEnabled(false);
		} else {
			for (User aUser : my_conference.getUsers()) {
				my_userComboBox.addItem(aUser.getUserName());
			}
			
			my_userComboBox.addActionListener(new ActionListener() {
				public void actionPerformed(final ActionEvent event) {
					final String selectedUser = 
						(String) my_userComboBox.getSelectedItem();
					if (selectedUser == null) {
						my_roleComboBox.setEnabled(false);
						my_goButton.setEnabled(false);
					} else {
						my_user = my_conference.getUser(selectedUser);
						my_roleComboBox.setEnabled(true);
						roleCBInit();
					}
				}
			});
		}
	}
	
	/**
	 * Role ComboBox Initialization
	 */
	private void roleCBInit() {
		my_roleComboBox.removeAllItems();
		my_roleComboBox.addItem("Select a Role...");
		if (my_conference == null || my_user == null){
			my_roleComboBox.setEnabled(false);
		} else {
			for (EnumRole role : my_user.getRoles()) {
				my_roleComboBox.addItem(role);
			}
			
			my_roleComboBox.addActionListener(new ActionListener(){
				public void actionPerformed(final ActionEvent event) {
					Object role = 
						my_roleComboBox.getSelectedItem();
					
					if (!(role instanceof EnumRole)) {
						my_goButton.setEnabled(false);
					} else {
						my_role = (EnumRole) role;
						my_goButton.setEnabled(true);
					}
				}
			});
		}
	}
	
	/**
	 * Go Button Initialization
	 */
	private void goButtonInit() 
	{
		if (my_conference == null || my_user == null ||
				my_role == null) {
			my_goButton.setEnabled(false);
		}
		
		my_goButton.setFont(
				new Font("Times New Roman", 
						Font.ITALIC + Font.BOLD, 18));
		my_goButton.setBackground(
				new Color(255, 255, 255, 0));
		my_goButton.setText("Go!");
		
		my_goButton.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent event) {
				my_user.setCurrentRole(my_role);
				my_parent.display(my_conference, my_user, my_role);
			}
		});
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void paintComponent(final Graphics the_graphics) {
		super.paintChildren(the_graphics);
		final Graphics2D g2d = (Graphics2D) the_graphics;
		
		my_parentPanel.repaint();
	}
}
